function checkEmpty( pole )
{
    if ( pole.value == "")
    {
        return false;
    }
    else
    {
        return true;
    }
}

function checkForm()
{
    var firstName = document.getElementById( 'firstName' );
    var lastName = document.getElementById( 'lastName' );
    var email = document.getElementById( 'email' );
    var reEmail = document.getElementById( 'reEmail' );
    var password = document.getElementById( 'password' );
    var select_sex = document.getElementById( 'select_sex' );
    var month = document.getElementById( 'month' );
    var day = document.getElementById( 'day' );
    var year = document.getElementById( 'year' );
    var isValid = ( checkEmpty( firstName ) && checkEmpty( lastName ) && checkEmpty( email ) 
											&& checkEmpty( reEmail ) && checkEmpty( password ) 
											&& ( select_sex.value != "Select Sex:" ) && ( month.value != "Month:" ) 
											&& ( day.value != "Day:" ) && ( year.value != "Year:" ) );
	
    var isSimilar = ( email.value == reEmail.value );
	var nameSymbols = /^[a-zA-Z]+$/;
	var emailSymbols = /^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/;
	
	var isCorrectName = ( nameSymbols.test( firstName.value ) && nameSymbols.test( lastName.value ) );
	var isCorrectEmail = emailSymbols.test( email.value );
	
    if ( isValid )
    {
        if ( isCorrectName )
		{
			if ( isSimilar )
			{
				if ( isCorrectEmail )
				{
					if ( password.value.length > 5 )
					{
						return true;
					}
					else
					{
						alert("Simple password!");
						return false;
					}
				}
				else
				{
					alert("Uncorrect email!");
					return false;
				}
			}
			else
			{
				alert("Re-entering of email was incorrect!");
				return false;
			}
		}
		else
		{
			alert("Uncorrect name!");
			return false;
		}
    }
    else
    {
        alert("Not all data is entered!");
        return false;
    }
}


function showElement()
{
	var button = document.getElementById( 'menu' );
	var menu = document.getElementById( 'float_menu' );
	menu.style.display = "inline-block";
	menu.backgroundPosition = "top";
	button.onmouseout = function()
	{ 
		menu.style.display = "none";
	}
}


function printLevel( resultXml )
{
	var status = document.getElementById('passStatus');
	if(resultXml == '0')
	{
		status.innerHTML = "empty";
		status.style.color = "blue";
	}
	if(resultXml == '1')
	{
		status.innerHTML = "weak";
		status.style.color = "red";
	}
	if(resultXml == '2')
	{
		status.innerHTML = "good";
		status.style.color = "yellow";
	}
	if(resultXml == '3')
	{
		status.innerHTML = "strong";
		status.style.color = "green";
	}
	if(resultXml == '-1')
	{
		status.innerHTML = "password must include only A-z, 0-9";
		status.style.color = "blue";
	}
}

function getSafety()
{
	var req = getXmlHttp();
	var pass = document.getElementById('password').value;
	req.open("GET", "/php/check_safety_password.php?password=" + pass, true);
	req.onreadystatechange = function()
	{
		if( (req.readyState == 4) && (req.status == 200) )
		{
			printLevel( req.responseText );
		}
	}
	req.send( null );	
}

function changeRating(action, id)
{
	var req = getXmlHttp();
	req.open("GET", "/php/change_rating.php?id=" + id + "&action=" + action, true);
	req.onreadystatechange = function()
	{
		if( (req.readyState == 4) && (req.status == 200) )
		{
			var rate = document.getElementById("file_" + id + "");
			rate.innerHTML = req.responseText;
		}
	}
	req.send(null);
}

function changeImage( img )
{
	var button = getElementById('startSlide');
	button.style.display = "none";
	var req = getXmlHttp();
	req.open("GET", "/php/slide_show.php?img=" + img, true);
	req.onreadystatechange = function()
	{
		if( (req.readyState == 4) && (req.status == 200) )
		{
			alert( req.responseText );
		}
	}
	req.send(null);
}